﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Common.Library;
using OnlineVideos.Common;
using OnlineVideos.UI;
using OnlineVideos.Library;
using OnlineVideos.View_Models;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using MarkHeath.StarRating;
using System.ComponentModel;
using OnlineVideos.Data;
using Common;
using Windows.Phone.Speech.Synthesis;
using System.Threading.Tasks;
using OnlineVideos.Entities;
namespace OnlineVideos
{
    public partial class ShowDescription : UserControl
    {   
        #region Global
         SpeechSynthesizer _synthesizer;
           
        ShowList MovieDetails = null;
        ShowDetail showdetail;
         #endregion
        public ShowDescription()
        {
            InitializeComponent();
            showdetail = new ShowDetail();
            MovieDetails = new ShowList();
           
        }
        private void LoadDescriptionSection(long showID)
        {
            try
            {
            BackgroundHelper bwHelper = new BackgroundHelper();

            bwHelper.AddBackgroundTask(
                                        (object s, DoWorkEventArgs a) =>
                                        {

                                            a.Result = OnlineShow.GetShowDetail(showID);
                                        },
                                        (object s, RunWorkerCompletedEventArgs a) =>
                                        {
                                            MovieDetails = (ShowList)a.Result;
                                            if (MovieDetails.ReleaseDate.Contains(','))
                                            {
                                                tblRelease.Text = MovieDetails.ReleaseDate.Substring(MovieDetails.ReleaseDate.LastIndexOf(',') + 1);

                                            }
                                            else if (ResourceHelper.ProjectName == "Indian Cinema" || ResourceHelper.ProjectName == "Indian Cinema Pro" || ResourceHelper.ProjectName == "Bollywood Movies" || ResourceHelper.ProjectName == "Bollywood Music")
                                            {
                                                tblRelease.Text = MovieDetails.ReleaseDate.Substring(MovieDetails.ReleaseDate.Length - 4);
                                            }
                                            else
                                            {
                                                tblRelease.Text = MovieDetails.ReleaseDate.Replace("12:00:00 AM", "");
                                            }
                                            tblkTitle.Text = MovieDetails.Title;
                                            
                                            double rateindconverted = Convert.ToDouble(MovieDetails.Rating) * 2;
                                            rate.Rating = Convert.ToInt32(rateindconverted);
                                            if (!string.IsNullOrEmpty(MovieDetails.Description))
                                            {
                                                tblkDescription.Text = MovieDetails.Description;
                                                imgShare.Margin = new Thickness(30, 0, 90, 0);
                                                imgFav.Margin = new Thickness(-40, 0, 0, 0);
                                                imgSpeech.Visibility = Visibility.Visible;
                                            }
                                            else
                                            {
                                                tblkDescription.Visibility = Visibility.Collapsed;
                                                tblkNoDescription.Visibility = Visibility.Visible;
                                                tblkNoDescription.Text = "no description available";
                                            }

                                            bool img = ShellTileHelper.IsPinned(AppSettings.ShowID);
                                                if (img == true)
                                            {
                                                imgpin.Source = new BitmapImage(new Uri("/Images/unpin.png", UriKind.Relative));
                                            }
                                            else
                                            {
                                                imgpin.Source = new BitmapImage(new Uri("/Images/pin.png", UriKind.Relative));
                                            }
                                            //TODO: Probably need to add relative path prefix.
                                            imgMovie.Source = ImageHelper.LoadTileImage(MovieDetails.TileImage);
                                            imgShare.Tag = MovieDetails.Title;
                                            imgFav.Source = ResourceHelper.getShowFavoriteStatusImage(MovieDetails.IsFavourite);

                                            
                                        }
                                      );

            bwHelper.RunBackgroundWorkers();
        }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in LoadDescriptionSection method In ShowDescription.cs file.", ex);
            }
        }
        private void rate_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            StarRatingControl starratingcontrol = new StarRatingControl();
            starratingcontrol.Rating = rate.Rating;
            showdetail.RateThisShow(starratingcontrol);
        }
        private void imgShare_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
            ShareLinkTask shareLinkTask = new ShareLinkTask();
            shareLinkTask.Title = ResourceHelper.ProjectName + " App";
            shareLinkTask.Message = " Watch bollywood movie '" + imgShare.Tag + "', Get the app at \n";
            shareLinkTask.LinkUri = ResourceHelper.AppMarketplaceWebLink;
            shareLinkTask.Show();
        }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in imgShare_MouseLeftButtonDown method In ShowDescription.cs file.", ex);
            }
        }

        private void imgFav_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
            FavoritesManager.SaveFavorites(AppSettings.ShowUniqueID);

            if (FavoritesManager.IsFavoriteShow(AppSettings.ShowUniqueID))
                imgFav.Source = ResourceHelper.RemoveFromFavoritesImage;
            else
                imgFav.Source = ResourceHelper.AddToFavoritesImage;
        }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in imgFav_MouseLeftButtonDown method In ShowDescription.cs file.", ex);
            }
        }

        private void imgpin_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
            ShellTileHelper.PinMovieToStartScreen(AppSettings.ShowID);
            bool img = ShellTileHelper.IsPinned(AppSettings.ShowID);
            if (img == true)
            {
                imgpin.Source = new BitmapImage(new Uri("/Images/unpin.png", UriKind.Relative));
            }
            else
            {
                imgpin.Source = new BitmapImage(new Uri("/Images/pin.png", UriKind.Relative));
            }
        }
            catch (Exception ex)
            {
                  Exceptions.SaveOrSendExceptions("Exception in imgpin_MouseLeftButtonDown method In ShowDescription.cs file.", ex);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
            LoadDescriptionSection(AppSettings.ShowUniqueID);
            Constants.Synthesizer = null;
        }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in UserControl_Loaded method In ShowDescription.cs file.", ex);
            }
        }

        public void PinOrUnpinShow(Image PinUnpinIcon)
        {
            try
            {
            bool IsMoviePinned;

            ShowList show = OnlineShow.GetShowDetail(Convert.ToInt32(AppSettings.ShowID));

            IsMoviePinned = ShellTileHelper.IsPinned(AppSettings.ShowID);
            if (IsMoviePinned)
            {
                ShellTileHelper.UnPin(AppSettings.ShowID);
                Storage.DeleteSecondaryTileImage(show.TileImage);
                if (PinUnpinIcon != null)
                    PinUnpinIcon.Source = ResourceHelper.PinImage;
            }
            else
            {
                ShellTileHelper.PinShowToStartScreen(show);

                if (PinUnpinIcon != null)
                    PinUnpinIcon.Source = ResourceHelper.UnpinImage;
            }
            }
            catch (Exception ex)
            {
                
                Exceptions.SaveOrSendExceptions("Exception in PinOrUnpinShow method In ShowDescription.cs file.", ex);
        }
        }

        private void imgSpeech_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            try
            {
            if (Constants.Synthesizer == null)
            {
                imgSpeech.Source = ResourceHelper.StopSpeech;
                Constants.Synthesizer = new SpeechSynthesizer();
                string ReadText = tblkDescription.Text;
                    Task.Run(async () => await Constants.Synthesizer.SpeakTextAsync(ReadText, 1));
            }
            else
            {
                imgSpeech.Source = ResourceHelper.StartSpeech;
                Constants.Synthesizer.Dispose();
                Constants.Synthesizer = null;
            }
            }
            catch (Exception ex)
            {
                
                 Exceptions.SaveOrSendExceptions("Exception in imgSpeech_MouseLeftButtonDown method In ShowDescription.cs file.", ex);
            }
           
          
        }

    }
}
